﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Agents.Data;

namespace Agents.Agent
{
    public partial class QueryNoMap : System.Web.UI.Page
    {
        public string[] Locations= {"1"};
        public string[] names = { "1" };
        public string[] dates = { "1" };
        public double[] totalOrders = { 1 };
        public double[] totalMoney = { 1 };
        public string[] priority = { "1" };
        public int size=1;
        public string addr, agentName;
        public double? pri, last, total, avg, dist;
        protected void Page_Load(object sender, EventArgs e)
        {
            Queries queries = new Queries();

            Agents agent = queries.GetAgentByUserName(Page.User.Identity.Name);
            agentName = agent.Name;
           
            addr= Request.QueryString["address"];
            Locations = new string[1];
            names = new string[1];
            dates = new string[1];
            totalOrders= new double[1];
            totalMoney = new double[1];
            priority = new string[1];
            int i = 1;
            size = 0;
            Model1Container contex = new Model1Container();
            var result =
                (from c in contex.Clients
                 select c);
            foreach (Clients c in result)
            {
                System.Array.Resize<string>(ref Locations, i);
                System.Array.Resize<string>(ref names, i);
                System.Array.Resize<string>(ref dates, i);
                System.Array.Resize<double>(ref totalOrders, i);
                System.Array.Resize<double>(ref totalMoney, i);
                System.Array.Resize<string>(ref priority, i);

                Locations[i - 1] = c.GetAddressInESRIFormat().Replace("'", "");
                dates[i - 1] = c.LastOrder.ToString();

                // Escape characters on names
                names[i - 1] = c.Name.Replace("'", "");

                totalOrders[i - 1] = c.TotalOrders;
                totalMoney[i - 1] = c.AvgOrder;
                priority[i - 1] = c.Priority;
                int num = i - 1;
                //ClientScript.RegisterHiddenField("hArray"+num, Locations[i-1]);
                i++;
                size++;
                //if(i==4)
                //empty.Text = c.Adress;
            }

            var result2 =
                (from md in contex.Configuration
                 select md);
            foreach (Configuration md in result2)
            {
                
                pri = md.Priority;
                avg = md.AvgMoney;
                total = md.TotalOrders;
                dist = md.Distance;
                last = md.LastOrder;
            }

            // Try and get location based notifications
            if (IsPostBack)
            {
                // Get the agent's location if needed location based notifications
                if (!queries.GetConfiguration().AllowLocationBasedNotifications)
                {
                    updatedAgentLocation.Value = "true";
                }
                else if ((!String.IsNullOrEmpty(hiddenLocationX.Value)) &&
                    (!String.IsNullOrEmpty(hiddenLocationY.Value)) &&
                    (updatedAgentLocation.Value == "false"))
                {
                    double x = Double.Parse(hiddenLocationX.Value);
                    double y = Double.Parse(hiddenLocationY.Value);

                    List<LocationBasedNotifications> notifications =
                        queries.GetLocationBasedNotifications(x, y);

                    updatedAgentLocation.Value = "true";

                    // Show the popup of notifications
                    if (notifications.Count > 0)
                    {
                        String notificationsIds =
                            String.Join(",", notifications.Select((n) => n.Id));

                        String script = String.Format(
                            "<script type=text/javascript>" +
                            "PopupCenter('NotificationDetails.aspx?notificationsIds={0}'," +
                            "'מערכת סוכנים',630, 330, false);</script>", notificationsIds);

                        ClientScript.RegisterStartupScript(this.GetType(), "onload", script);
                    }
                }
            }
        }

        [WebMethod]
        public static void GoingTo(string clientName, string agentName)
        {
            Queries queries = new Queries();
            
            queries.ChangeGoingTo(queries.GetAgent(agentName), 
                queries.GetClient(clientName)); 
        }

        protected void btn_Click1(object sender, EventArgs e)
        {
            Queries queries = new Queries();

            // Get the agent who is in the page
            Agents agent = queries.GetAgentByUserName(Page.User.Identity.Name);

            // Get the client the agent is at
            Response.Redirect("StartShiftDisplay.aspx?clientId=" + queries.GetClient(agent.Status).Id);
        }
    }
}